package com.foreveross.crawl.common.cfg;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.foreveross.crawl.common.util.ClassUtils;
import com.foreveross.crawl.common.util.convert.BooleanConvert;
import com.foreveross.crawl.common.util.convert.DoubleConvert;
import com.foreveross.crawl.common.util.convert.FloatConvert;
import com.foreveross.crawl.common.util.convert.IntegerConvert;
import com.google.common.collect.Lists;

/**
 * 配置文件帮助类
 * <p>包含一些公共的静态方法，通过调用这些方法可以获得指定格式的值
 * @author xiangsf 2012-8-29
 *
 */
public final class PropertyUtils {

	public static String getProperty(String key, String file){
		return ConfigContainer.getInstance().get(key, file);
	}	
	
	public static String[] getArrayPropertys(String key, String file, String separator){
		String value = ConfigContainer.getInstance().get(key, file);
		return value.split(separator);
	}	
	public static List<String> getListPropertys(String key, String file, String separator){
		String value = ConfigContainer.getInstance().get(key, file);
		return Lists.newArrayList(value.split(separator));
	}	
	public static String getProperty(String key, String file, String defaultValue){
		String value = getProperty(key, file);
		return StringUtils.isEmpty(value) ? defaultValue : value;
	}	
	
	public static Boolean getBooleanProperty(String key, String file){
		String value = ConfigContainer.getInstance().get(key, file);
		return BooleanConvert.convert(value);
	}
	
	public static Double getDoubleProperty(String key,String file){
		String value = ConfigContainer.getInstance().get(key, file);
		return DoubleConvert.convert(value);
	}
	public static Double getDoubleProperty(String key,String file,double defaultValue){
		String value = ConfigContainer.getInstance().get(key, file);
		return DoubleConvert.convert(value, defaultValue);
	}
	public static Float getFloatProperty(String key, String file){
		String value = ConfigContainer.getInstance().get(key, file);
		return FloatConvert.convert(value);
	}
	public static Float getFloatProperty(String key, String file, float defaultValue){
		String value = ConfigContainer.getInstance().get(key, file);
		return FloatConvert.convert(value,defaultValue);
	}
	
	public static Integer getIntProperty(String key,String file){
		String value = ConfigContainer.getInstance().get(key, file);
		return IntegerConvert.convert(value);
	}
	public static Integer getIntProperty(String key,String file, int defaultValue){
		String value = ConfigContainer.getInstance().get(key, file);
		return IntegerConvert.convert(value, defaultValue);
	}
	
	public static Integer[] getIntPropertys(String key,String file, String separator){
		String value = ConfigContainer.getInstance().get(key, file);
		Integer[] retV = {};
		if(StringUtils.isNotEmpty(value)){
			String[] v = value.split(separator);
			retV = IntegerConvert.convert(v);
		}
		return retV;
	}
	public static Object getBeanProperty(String key,String file){
		Object obj = null;
		String value = ConfigContainer.getInstance().get(key, file);

		if(StringUtils.isNotEmpty(value)){
			obj = ClassUtils.newInstance(value, new Object[]{});
		}
		return obj;
	}
	
}
